package com.qst.rongxiaotong.controller;

import com.github.pagehelper.PageInfo;
import com.qst.rongxiaotong.common.Result;
import com.qst.rongxiaotong.common.StatusCode;
import com.qst.rongxiaotong.pojo.Knowledge;
import com.qst.rongxiaotong.service.KnowledgeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "农业知识模块接口")
@RestController
@RequestMapping("/knowledge")
@CrossOrigin
public class KnowledgeController {
    @Autowired
    private KnowledgeService knowledgeService;

    @ApiOperation(value = "分页查询所有农业知识")
    @GetMapping("/{pageNum}")
    public Result<PageInfo<Knowledge>> findPage(@PathVariable Integer pageNum){
        PageInfo<Knowledge> page = knowledgeService.findPage(pageNum);
        Result<PageInfo<Knowledge>> pageInfoResult = new Result<>(true, StatusCode.OK, "查询成功", page);
        return pageInfoResult;
    }

    @ApiOperation(value = "根据id查询农业知识")
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        Knowledge knowledge = knowledgeService.selectById(id);
        Result result = new Result(true, StatusCode.OK, "查询成功", knowledge);
        return result;
    }

    @ApiOperation(value = "分页条件查询所有农业知识")
    @GetMapping("/{keys}/{pageNum}")
    public Result<PageInfo<Knowledge>> findPageByKeys(@PathVariable String keys,@PathVariable Integer pageNum){
        PageInfo<Knowledge> pageByKeys = knowledgeService.findPageByKeys(keys, pageNum);
        Result result = new Result(true, StatusCode.OK, "查询成功", pageByKeys);
        return result;
    }


    //TODO 根据登录用户查询知识--查询成功，但用户名是写死的，目前无法根据登录用户来查询
    @ApiOperation(value = "根据登录用户查询知识")
    @GetMapping("/selectByUsername")
    public Result selectByUsername(){
        //获取用户的登录名
        //UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //String username = userDetails.getUsername();

        //List<Knowledge> knowledges = knowledgeService.selectByUsername("gaoge");
        //List<Knowledge> knowledges = knowledgeService.selectByUsername("zhangxukun");
        List<Knowledge> knowledges = knowledgeService.selectByUsername("测试1");
        Result result = new Result(true, StatusCode.OK, "查询成功", knowledges);
        return result;
    }

    @ApiOperation(value = "添加知识")
    @PostMapping()
    @PreAuthorize("hasAuthority('expert')")
    public Result add(@RequestBody Knowledge knowledge){
        knowledgeService.add(knowledge);
        Result result = new Result(true, StatusCode.OK, "添加成功");
        return result;
    }

    @ApiOperation(value = "根据id修改知识")
    @PutMapping("/{id}")
    public Result update(@RequestBody Knowledge knowledge, @PathVariable("id") Integer id){
        knowledgeService.update(knowledge,id);
        Result result = new Result(true, StatusCode.OK, "修改成功");
        return result;
    }

    @ApiOperation(value = "根据id删除知识")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        knowledgeService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }





}
